package com.net.wanjian.networkhospitalmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.adapter.SelectHospitalAdapter;
import com.net.wanjian.networkhospitalmanager.bean.GetHospitalListReturn;
import com.net.wanjian.networkhospitalmanager.consts.SharedPreferencesKeyConst;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.ProgressDialogUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

/**
 * 选择医院
 */
public class SelectHospitalActivity extends BaseActivity {
    public final static int REQUEST_CODE = 0x123;
    public final static String REQUEST_TYPE = "com.net.wanjian.activity.selecthospitalactivity.request_type";

    @BindView(R.id.select_hospital_close_tv)
    TextView selectHospitalCloseTv;
    @BindView(R.id.select_hospital_search_et)
    EditText selectHospitalSearchEt;
    @BindView(R.id.select_hospital_expandable_list)
    ExpandableListView selectHospitalExpandableList;

    private SelectHospitalAdapter hospitalAdapter;
    private String requestType;
    private GetHospitalListReturn.DataBean listReturnData = new GetHospitalListReturn.DataBean();
    private GetHospitalListReturn.DataBean searchReturnData = new GetHospitalListReturn.DataBean();
    private List<GetHospitalListReturn.DataBean.DataInfoListBean> searchInfoList = new ArrayList<>();
    private List<GetHospitalListReturn.DataBean.DataInfoListBean.CustomerInfoListBean> customerList = new ArrayList<>();


    @Override
    protected void initData() {

        Bundle bundle = getIntent().getExtras();
        if (null != bundle)
            requestType = bundle.getString(REQUEST_TYPE);

        selectHospitalCloseTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectHospitalActivity.this.finish();
            }
        });
        selectHospitalSearchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                searchInfoList.clear();

                customerList.clear();

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //此处进行搜索匹配
                int size = 0;
                if (null != listReturnData) {
                    size = listReturnData.getDataInfoList().size();
                } else {
                    return;
                }

                if (s.toString().isEmpty()) {
                    //设置searchreturndata值为全部
                    searchReturnData.setDataInfoList(listReturnData.getDataInfoList());
                    hospitalAdapter.setListData(searchReturnData);

                    for (int i = 0; i < size; i++) {
                        selectHospitalExpandableList.expandGroup(i);
                    }

                    return;
                }

                //搜索省份相同或城市名字匹配

                for (int i = 0; i < size; i++) {
                    customerList.clear();
                    if (URLDecoderUtil.getDecoder(listReturnData.getDataInfoList().get(i).getProvince()).contains(s)) {
                        searchInfoList.add(listReturnData.getDataInfoList().get(i));
                    } else {
                        for (int j = 0; j < listReturnData.getDataInfoList().get(i).getCustomerInfoList().size(); j++) {
                            if (URLDecoderUtil.getDecoder(listReturnData.getDataInfoList().get(i).getCustomerInfoList().get(j).getCustomerInfoName()).contains(s)) {
                                customerList.add(listReturnData.getDataInfoList().get(i).getCustomerInfoList().get(j));
                            }
                        }
                        List<GetHospitalListReturn.DataBean.DataInfoListBean.CustomerInfoListBean> customerList2 = new ArrayList<>();
                        customerList2.addAll(customerList);
                        if (!customerList2.isEmpty()) {
                            GetHospitalListReturn.DataBean.DataInfoListBean listBean = new GetHospitalListReturn.DataBean.DataInfoListBean();
                            listBean.setProvince(listReturnData.getDataInfoList().get(i).getProvince());
                            listBean.setCustomerInfoList(customerList2);
                            searchInfoList.add(listBean);
                        }
                    }
                }

                searchReturnData.setDataInfoList(searchInfoList);
                hospitalAdapter.setListData(searchReturnData);

                for (int i = 0; i < searchReturnData.getDataInfoList().size(); i++) {
                    selectHospitalExpandableList.expandGroup(i);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
//        if (!NetUtils.isNetworkConnectedShowToast(this)){
//            return;
//        }
        ProgressDialogUtils.showProgressDialog(this, null, R.string.loading_text);
        //获取数据
        HttpUtil.getHospitalList(new DisposableObserver<GetHospitalListReturn>() {
            @Override
            public void onNext(GetHospitalListReturn getHospitalListReturn) {
                if (getHospitalListReturn.getResult().equals("1")) {
                    setExpandList(getHospitalListReturn);
                }
            }

            @Override
            public void onError(Throwable e) {
                ProgressDialogUtils.dismissProgressDialog();
            }

            @Override
            public void onComplete() {
                ProgressDialogUtils.dismissProgressDialog();
            }
        });
    }


    /**
     * 设置展开
     *
     * @param listReturn
     */
    private void setExpandList(final GetHospitalListReturn listReturn) {

        listReturnData.setDataInfoList(listReturn.getData().getDataInfoList());
        searchReturnData = listReturn.getData();

        hospitalAdapter = new SelectHospitalAdapter(this, listReturn.getData());
        selectHospitalExpandableList.setAdapter(hospitalAdapter);

        //设置展开
        if (null == listReturn.getData().getDataInfoList()) return;
        for (int i = 0; i < listReturn.getData().getDataInfoList().size(); i++) {
            selectHospitalExpandableList.expandGroup(i);
        }
        selectHospitalExpandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });

        selectHospitalExpandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                String customerInfoName = URLDecoderUtil.getDecoder(searchReturnData.getDataInfoList().get(groupPosition).getCustomerInfoList().get(childPosition).getCustomerInfoName());
                String customerInfoID = URLDecoderUtil.getDecoder(searchReturnData.getDataInfoList().get(groupPosition).getCustomerInfoList().get(childPosition).getCustomerInfoID());
                String aliVideoID = URLDecoderUtil.getDecoder(searchReturnData.getDataInfoList().get(groupPosition).getCustomerInfoList().get(childPosition).getCustomerInfoAliVideoClassID());
                if ("login".equals(requestType)) {
                    Intent intent = new Intent();
                    intent.putExtra("hospital", customerInfoName);
                    setResult(REQUEST_CODE + 1, intent);
                    SharedXmlUtil.getInstance().write(SharedPreferencesKeyConst.SELECT_HOSPITAL_KEY, customerInfoID);
                    SharedXmlUtil.getInstance().write(SharedPreferencesKeyConst.SELECT_HOSPITAL_NAME, customerInfoName);
                    SharedXmlUtil.getInstance().write(SharedPreferencesKeyConst.SELECT_ALI_VIDEO_ID, aliVideoID);

                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString(LoginActivity.HOSPITALID_KEY, customerInfoID);
                    bundle.putString(LoginActivity.HOSPITALID_NAME, customerInfoName);
                    openActivity(LoginActivity.class, bundle);
                    SharedXmlUtil.getInstance().write(SharedPreferencesKeyConst.SELECT_HOSPITAL_KEY, customerInfoID);
                    SharedXmlUtil.getInstance().write(SharedPreferencesKeyConst.SELECT_HOSPITAL_NAME, customerInfoName);
                    SharedXmlUtil.getInstance().write(SharedPreferencesKeyConst.SELECT_ALI_VIDEO_ID, aliVideoID);

                }


                SelectHospitalActivity.this.finish();
                return true;
            }
        });

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_hospital;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

}
